﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Nizm0.QOT.Library.Options
{
    public partial class RegexOptions : UserControl, Library.Objects.IOption
    {
        public RegexOptions()
        {
            InitializeComponent();
            Fill();
        }

        private void bt_Add_Click(object sender, EventArgs e)
        {
            if (!Settings.Instance.PaternList.Exists(p => p.Pattern == @"--\s*(V|v|ver|VER|Ver)([\.\-\\\s])*(?<Ver>([0-9]{1,3})([\.\-\\\s])([0-9]{1,3})([\.\-\\\s])([0-9]{1,3}))[\s\|]*(?<Data>[0-9]{4}-[0-9]{2}-[0-9]{2})[\s\|]*(?<Project>[^\||^\s|^\r]+)[\s\|\s]*(?<User>[A-Z]{1}[^\s]*\s[A-Z]{1}[^\s|\|]*){0,}[\s\|\s]*(?<Comment>[^\r]+)"))
                Settings.Instance.PaternList.Add(new Objects.RegexPatern("procedure", @"--\s*(V|v|ver|VER|Ver)([\.\-\\\s])*(?<Ver>([0-9]{1,3})([\.\-\\\s])([0-9]{1,3})([\.\-\\\s])([0-9]{1,3}))[\s\|]*(?<Data>[0-9]{4}-[0-9]{2}-[0-9]{2})[\s\|]*(?<Project>[^\||^\s|^\r]+)[\s\|\s]*(?<User>[A-Z]{1}[^\s]*\s[A-Z]{1}[^\s|\|]*){0,}[\s\|\s]*(?<Comment>[^\r]+)"));
            if (!Settings.Instance.PaternList.Exists(p => p.Pattern == @"--\s*(?<Ver>([0-9]{1,3}(\.)*)+)[\s\|]*(?<Data>[0-9]{4}-[0-9]{2}-[0-9]{2})[\s\|]*(?<Project>[^\||^\s|^\r]+)[\s\|]*(?<User>[A-Z]{1}[^\s]*\s[A-Z]{1}[^\s|\|]*){0,}[\s\|\s]*(?<Comment>[^\r]+)"))
                Settings.Instance.PaternList.Add(new Objects.RegexPatern("procedure", @"--\s*(?<Ver>([0-9]{1,3}(\.)*)+)[\s\|]*(?<Data>[0-9]{4}-[0-9]{2}-[0-9]{2})[\s\|]*(?<Project>[^\||^\s|^\r]+)[\s\|]*(?<User>[A-Z]{1}[^\s]*\s[A-Z]{1}[^\s|\|]*){0,}[\s\|\s]*(?<Comment>[^\r]+)"));

            listBox1.DataSource = Settings.Instance.PaternList;
            ((CurrencyManager)listBox1.BindingContext[Settings.Instance.PaternList]).Refresh();
        }

        private void bt_Dell_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count != 0)
            {
                Settings.Instance.PaternList.RemoveAll(r => r == listBox1.SelectedItem);
                ((CurrencyManager)listBox1.BindingContext[Settings.Instance.PaternList]).Refresh();
            }
        }

        public string OptionPath
        {
            get
            {
                return "Main Settings\\Regex";
            }
        }

        public string OptionName
        {
            get { return "Regex"; }
        }

        private bool _optionChange = true;
        public bool OptionChanged
        {
            get { return _optionChange; }
        }

        public bool Save()
        {
            return SaveSettings();
        }

        private bool SaveSettings()
        {
            Settings.Instance.Save();
            return true;
        }

        public new Objects.IOption Show()
        {
            return new RegexOptions();
        }

        public void Fill()
        {
            listBox1.DataSource = Settings.Instance.PaternList;

            ((CurrencyManager)listBox1.BindingContext[Settings.Instance.PaternList]).Refresh();
        }
    }
}
